package player

import (
	"base"
	"gate_server/logic/game/event"
	. "gate_server/logic/internal"
	"gate_server/logic/player/api"
	"gate_server/logic/player/event"
	"gate_server/model"
	"proto/base"
)

type playerSystem struct {
	players map[base.PlayerId]*model.Player
}

var system = new(playerSystem)

func init() {
	system.players = make(map[base.PlayerId]*model.Player)
}

func init() {
	player_api.Get = system.Get
}

func init() {
	Event.Listen(game_event.OnClose, system.onServerClose)
}

func (*playerSystem) Get(playerId base.PlayerId) (result *model.Player, exists bool) {
	result, exists = system.players[playerId]
	return
}

func (*playerSystem) onServerClose(players []base.PlayerId) {
	for _, playerId := range players {
		if player, exists := system.Get(playerId); exists {
			const reason = proto_base.LogoutReason_Stop
			Event.Notify(player_event.OnLogout, player, reason)

			delete(system.players, playerId)
		}
	}
}
